﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;

namespace WebCMD
{
    public static class CMD
    {
        private static Process process;
        static CMD()
        {
            process = new Process();

            // process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
        }

        static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {

        }

        public static string Run(string cmd)
        {
            string output = "";
            try
            {
                ProcessStartInfo info = new ProcessStartInfo();
                info.UseShellExecute = false; 
                info.RedirectStandardOutput = true;
                if (string.IsNullOrEmpty(cmd))
                {
                    return output;
                }
                if (cmd.Contains(' '))
                {
                    info.FileName = cmd.Substring(0, cmd.IndexOf(' '));
                    info.Arguments = cmd.Substring(cmd.IndexOf(' '));
                }
                else
                {
                    info.FileName = cmd;
                }
                
                process.StartInfo = info;
                process.Start();
                process.WaitForExit();
                output = process.StandardOutput.ReadToEnd();
            }
            catch (Exception ex)
            {
                output = ex.Message;
            }

            return output;
        }
    }
}
